package cn.clq.org.controller;

import cn.clq.basic.annotation.PreAuthorize;
import cn.clq.basic.util.AjaxResult;
import cn.clq.basic.util.PageList;
import cn.clq.org.domain.Department;
import cn.clq.org.query.DepartmentQuery;
import cn.clq.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 部门表的接口
 * @date 2022/6/5 9:49
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    /**
     * @description:查询单个部门
     * @param: id
     * @return: cn.clq.org.domain.Department
     * @author clqlongbao
     * @date: 2022/6/5 9:53
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }
    /**
     * @description:查询全部部门
     * @param:
     * @return: java.util.List<cn.clq.org.domain.Department>
     * @author clqlongbao
     * @date: 2022/6/5 10:52
     */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }
    /**
     * @description:高级分页查询
     * @param: departmentQuery
     * @return: cn.clq.basic.util.PageList<cn.clq.org.domain.Department>
     * @author clqlongbao
     * @date: 2022/6/5 10:55
     */
    @PreAuthorize(name = "部门高级分页查询",value = "department:list")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
    /**
     * @description:根据id删除一个部门
     * @param: id
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:01
     */
    @PreAuthorize(name = "删除部门",value = "department:delete")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }
    /**
     * @description:判断是新增还是修改的接口
     * @param: department
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/5 11:05
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                //新增
                departmentService.save(department);
            }else{
                //修改
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统瓦了");
        }
    }

    /**
     * @description:批量删除
     * @param: ids
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/6 18:22
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }


    @GetMapping("/tree")
    public List<Department> queryDepartmentTree(){
        return departmentService.deptTree();
    }
}
